<?php
/**
 * Created by PhpStorm.
 * User: ruma
 * Date: 2019/1/21
 * Time: 15:22
 */

namespace app\admin\controller;


use app\common\controller\AdminBaseController;
use app\Excepiton\ParameterException;
use think\Db;

/**
 * 栏目管理
 * Class Category
 * @package app\admin\controller
 */
class Category extends AdminBaseController
{
    //定义数据表
    private $db;

    protected function _initialize()
    {
        parent::_initialize();
        //实例化模型
        $this->db = new \app\admin\model\Category();
    }

    //分类首页
    public function index(){

        //获取栏目数据
        $field = $this->db->getAllCate();
        $this->assign('field',$field);
        return $this->fetch();
    }

    //(添加)栏目
    public function store(){
        if (IS_POST){
            try{
                $this->db->store(input('post.'));
            }
            catch (ParameterException $e){
                $str = arrayToString($e->msg);
                $this->error($str);
            }
            $this->success('栏目添加成功','admin/Category/index');
        }
        return $this->fetch();
    }

    //添加子集
    public function addSon(){
        if (IS_POST){
            try{
                $this->db->store(input('post.'));
            }
            catch (ParameterException $e){
                $str = arrayToString($e->msg);
                $this->error($str);
            }
            $this->success('子栏目添加成功','admin/Category/index');
        }
        $cid = input('param.cate_id');
        $pInfo = $this->db->where('cate_id','=',$cid)->find();
        $this->assign('pInfo',$pInfo);
        return $this->fetch();
    }

    /**
     *编辑栏目
     *
     */
    public function edit(){
        //1.获取主页面传进来的id号
        $cid = input('param.cate_id');
        $oldData = $this->db->where('cate_id','=', $cid)->find()->toArray();
        $this->assign('oldData',$oldData);
        //2.获得所属栏目[不能包含自己和自己的子集数据]
        $cateData = $this->db->getCateEditData($cid);
        //3.修改数据
        if (IS_POST){
            try{
                $this->db->editCate();
            }
            catch (ParameterException $e){
                $str = arrayToString($e->msg);
                $this->error('更改失败,'.$str,'index');
            }
            $this->success('更改成功',url('index'));
        }
        $this->assign('cateData',$cateData);
        return $this->fetch();
    }


    /*
    *删除栏目
    */

    public function delCate(){
        try{
            $this->db->del();
        }
        catch (ParameterException $e){
            $str = arrayToString($e->msg);
            $this->error('删除栏目失败,'.$str);
        }
        $this->success('删除成功','index');
    }


    /*修改栏目排序*/
    public function changeSort(){
        if (IS_POST){
            $cate_sort = input('post.cate_sort');
            $cate_id = input('post.cate_id');
            Db::table('category')
                ->where('cate_id','=',$cate_id)
                ->update(['cate_sort'=>$cate_sort]);
            echo true;
        }
        echo false ;
    }

}